<?php

namespace addons\shopro\controller\wm;

use addons\shopro\exception\Exception;
use think\Db;
use addons\shopro\controller\Base as AddonsBase;
class Index extends AddonsBase
{

    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];


    function storeLists(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $page = $this->request->param('page');
            $one_category = $this->request->param('one_category');
            $two_category = $this->request->param('two_category');
            $user_id = $this->auth->id;
            if(empty($one_category)){
                $where['one_category'] = $one_category;
            }
            if(empty($two_category)){
                $where['two_category'] = $two_category;
            }

            $page = $page??1;
            $size = 10;
            $where = [];
            $stores = db('shopro_store')
                ->where($where)
                ->paginate($size)
                ->each(function($item, $key){
                    $item['nickname'] = 'think';
                    return $item;
                });

            $data = [
                'stores'=>$stores,

            ];

            $this->success('',$data);
        }
    }





    /**
     * 团长。商家 个人中心 资金统计
     * 王猛 2022-1-25 14:14:07
     */
    function getMoney(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $types = $this->request->param('types');
            $user_id = $this->auth->id;
            if(empty($types)) $this->error('系统开了个小差！');
            if(!in_array($types,['sj','pt'])) $this->error('系统开了个小差！');
            //团长累计佣金
            $map['wallet'] = array('gt',0);
            $map['wm_types'] = array('in',$types);
            $map['user_id'] = array('=',$user_id);
            $pt_money_all = db('shopro_user_wallet_log')
                ->where($map)
                ->sum('wallet');
            $map['wallet'] = array('lt',0);

            //团长已提现
            $pt_money_get = db('shopro_user_wallet_log')
                ->where($map)
                ->sum('wallet');
            $pt_money_get = abs($pt_money_get);

            //团长未提
            $pt_money_notget = $pt_money_all - $pt_money_get;

            $data = [
                'money_all'=>$pt_money_all,
                'money_get'=>$pt_money_get,
                'money_notget'=>$pt_money_notget,
            ];

            $this->success('',$data);
        }
    }


    /**
     * 店长、团长佣金流水
     */
    function groupOrder(){
        $params = $this->request->post();

        $types = isset($params['types']) ? $params['types'] : 'pt';
        $date = isset($params['date']) ? explode('-', $params['date']) : [];
        $start = isset($date[0]) && $date[0] ? strtotime($date[0]) : strtotime(date('Y-m') . '-01');
        $end = isset($date[1]) && $date[1] ? (strtotime($date[1]) + 86399) : strtotime(date('Y-m-d')) + 86399;
        $where = [];
        if($types=='sj'){
            $where['a.store_id'] = $params['store_id'];
        }
        $pagesize = 10;
        $page = $params['page']??1;
        $user_id = $this->auth->id;
        $lists = db('shopro_user_wallet_log')
            ->alias('a')
            ->join('shopro_order_item b','a.item_id=b.id')
            ->where($where)
            ->where(['a.user_id'=>$user_id,'a.wm_types'=>$types,'a.type'=>'xt','a.wallet_type'=>'money'])
            ->whereBetween('a.createtime', [$start, $end])
            ->field('a.id as id,b.activity_type,b.goods_title,b.goods_num,b.goods_sku_text,b.goods_image,b.goods_price,a.wallet,a.ext,a.memo,a.createtime,b.createtime as orderCreatetime,b.money_pt,b.money_pt3,b.money_pt2,b.money_pt1,b.money_sys,b.money_sj,b.domoneytime,b.bind_domoneytime,b.pt_domoneytime')
            ->order('a.createtime desc')
            ->paginate()->each(function($item, $key){
                $item['order_info'] = json_decode($item['ext'],true);
                if($item['activity_type']=='groupon') $item['status_name']='拼团订单';
                if($item['activity_type']=='seckill') $item['status_name']='积分订单';
                if($item['activity_type']=='' || $item['activity_type']==NULL) $item['status_name']='常规订单';
                return $item;
            });

        $income = db('shopro_user_wallet_log')
            ->alias('a')
            ->join('shopro_order_item b','a.item_id=b.id')
            ->where($where)
            ->where(['a.user_id'=>$user_id,'a.wm_types'=>$types,'a.type'=>'xt','a.wallet_type'=>'money'])
            ->whereBetween('a.createtime', [$start, $end])
            ->field('a.id as id,b.activity_type,b.goods_title,b.goods_num,b.goods_sku_text,b.goods_image,b.goods_price,a.wallet,a.ext,a.memo,a.createtime,b.createtime as orderCreatetime,b.money_pt,b.money_pt3,b.money_pt2,b.money_pt1,b.money_sys,b.money_sj,b.domoneytime,b.bind_domoneytime,b.pt_domoneytime')
            ->sum('wallet');

        $res = [
            'lists'=>$lists,
            'wm_types'=>$types,
            'income'=>$income
        ];
        $this->success('',$res);
    }


}